package zj.boxuegu.administrator.myapplication.Fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;

import zj.boxuegu.administrator.myapplication.Activity.ExerciseModel;
import zj.boxuegu.administrator.myapplication.Activity.NewsActivity;
import zj.boxuegu.administrator.myapplication.R;

/**
 * Created by Administrator on 2018/4/2.
 */

public class ExerciseFragment extends Fragment {
    String exercise;


   // ExerciseModel exerciseModel;
    RecyclerView recyclerView1;
   public  ArrayList<ExerciseModel> list=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        exercise1();
        View view=inflater.inflate(R.layout.fragment_exercise, container, false);



        recyclerView1=view.findViewById(R.id.RecyclerView1);

        return view;
    }

    public void exercise1()

    {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd","7");
        params.addQueryStringParameter("v1","274");
        params.addQueryStringParameter("v2","1");
        params.addQueryStringParameter("v3","30");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


                 exercise=convertString(result);
                Log.i("exercise1 ","result"+result);
                Gson gson = new Gson();
                Type ListType =new TypeToken<ArrayList<ExerciseModel>>(){

                }.getType();

                if(gson==null){
                    gson = new Gson();
                }
                list=gson.fromJson(exercise,ListType);
                GridLayoutManager manager = new GridLayoutManager(getActivity(),1);
                recyclerView1.setLayoutManager(manager);
              //   exerciseModel= gson.fromJson(exercise, ExerciseModel.class);  //错误点


                recyclerView1 .setAdapter(new MyAdatper());




            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                Log.i("ExerciseFragment ","onSuccess"+exercise);

            }


        });


    }

    public  class MyAdatper extends  RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =View.inflate(getActivity(),R.layout.list_item,null);
            MyViewHolder myViewHolder =new MyViewHolder(view);

            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final ExerciseModel exerciseModel=list.get(position);
            holder.textView1.setText(exerciseModel.contentTitle);
            holder.textView2.setText(exerciseModel.submitTime);


            holder.CouserButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(), NewsActivity.class);
                    intent.putExtra("contentID",""+exerciseModel.contentID);
                    startActivity(intent);
                }
            });

        }



        @Override
        public int getItemCount() {
            return  list.size();

        }
    }
    public  class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView1,textView2;

        LinearLayout CouserButton2;
        public MyViewHolder(View itemView2) {
            super(itemView2);
            textView1=itemView2.findViewById(R.id.e_text1);
            textView2=itemView2.findViewById(R.id.e_text2);

            CouserButton2=itemView2.findViewById(R.id.CouserButton2);

        }
    }
    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");
        return result1;

    }

}









